import 'package:FineArt/components/rounded_input_field.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/grid/grid_item_photo.dart';
import 'package:FineArt/core/widget/grid/grid_item_photo.dart';
import 'package:FineArt/page/imageview/imageview.dart';
import 'package:FineArt/page/search/component/TextTagWidget.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

///搜索主页面
class SearchResultMainPage extends StatefulWidget {
  final String searchStr;

  SearchResultMainPage({this.searchStr});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _SearchResultMain();
  }
}

class _SearchResultMain extends State<SearchResultMainPage> {
  int _count = 3;
  int total;
  @override
  Widget build(BuildContext context) {
    buildItemUrl();
    String searStr = widget.searchStr;
    LogUtil.d("_SearchResultMain key $searStr");
    Size size = MediaQuery.of(context).size;
    double width = (size.width - 64) / 3;
    return Scaffold(
      body: Container(
        margin: EdgeInsets.fromLTRB(16, 30, 16, 0),
        decoration: BoxDecoration(color: Colors.white),
        alignment: Alignment.centerLeft,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                RoundedInputField(
                  icon:Image.asset("assets/images/icon_search.png",width: getPortraitSWidth(16.5),height: getPortraitSHeight(16.5),),
                  keyType: TextInputType.text,
                  hintText: "搜索素材",
                  height: 35,
                  text: searStr,
                  onChanged: (value) {
                    LogUtil.d("search val $value");
                  },
                ),
                InkWell(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Padding(
                      padding: EdgeInsets.only(top: 2, left: 10),
                      child: Text(
                        "取消",
                        style: TextStyle(
                          fontSize: 18,
                          color: Colors.red,
                        ),
                      ),
                    ))
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Row(
              children: [Text("共搜索到$total个素材")],
            ),
            Expanded(
                child: EasyRefresh.custom(
              header: MaterialHeader(),
              footer: MaterialFooter(),
              onRefresh: () async {
                await Future.delayed(Duration(seconds: 1), () {
                  setState(() {
                    _count = 5;
                  });
                });
              },
              onLoad: () async {
                await Future.delayed(Duration(seconds: 1), () {
                  setState(() {
                    _count += 5;
                  });
                });
              },
              slivers: <Widget>[
                SliverPadding(
                    padding: EdgeInsets.only(top: 2),
                    sliver: SliverGrid(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3,
                        mainAxisSpacing: 0,
                        crossAxisSpacing: 0,
                        childAspectRatio: 1,
                      ),
                      // gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                      //   mainAxisSpacing: 10.0,
                      //   crossAxisSpacing: 10.0,
                      //   childAspectRatio: 3.0,
                      //
                      // ),
                      delegate: SliverChildBuilderDelegate(
                        (BuildContext context, int index) {
                          //创建子widget
                          var action = actions[index];
                          return GridItemPhoto(
                              url: action.url,
                              width: width,
                              onTap: () {
                                ToastUtils.toast('点击-->${action.url}');
                                // ImageViewPage.JumpToImagePreview(
                                //     action.url, context);
                              });
                        },
                        childCount: actions.length,
                      ),
                    )),

                //=====列表=====//
              ],
            )),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  static const String TUrl =
      "https://cdn.rouding.cn/imagesrc-s/jpeg/201506-11-2B09E2AFEEABB4D4F23F2F118F6266E1-th.jpg";
  List<ActionPhotoItem> actions = [];

  void buildItemUrl() {
    for (int i = 0; i < 10; i++) {
      actions.add(ActionPhotoItem(TUrl));
    }
    setState(() {
      total= actions.length;
    });
  }
}

class ActionPhotoItem
{
  String url;
  ActionPhotoItem(this.url);
}
